#include<bits/stdc++.h>
#define LL long long
#define SZ(x) ((LL)(x.size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
inline LL read(){
  LL q=0,w=1;char ch=getchar();
  while(ch>'9' || ch<'0'){if(ch=='-')w=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){q=q*10+(ch-'0');ch=getchar();}
  return q*w;
}
void write(LL x){
  if(x<0){putchar('-');x=(-x);}
  if(x>9)write(x/10);
  putchar('0'+x%10);
}
inline void writeln(LL x){write(x);puts("");}
inline void writecs(LL x){write(x);putchar(' ');}

inline LL qpow(LL a,LL b,LL p){LL ans=1;while(b){if(b&1)ans=(ans*a)%p;b>>=1;a=(a*a)%p;}return ans;}
const LL mod = 998244353;
inline LL inv(LL x){return qpow(x,mod-2,mod);}

#define db long double
const db eps = (1e-10);

const LL N = 10000000+95;
LL TC,n,a[N],A,B,C,M,cnt[N];

inline bool check(db mid){
  db now=0.0;
  for(LL t=1,p=0;t<=M;t++){
    if(!cnt[t])continue;
    if(cnt[t]>1){
      if(!p)now=1.0-mid;
      else {
	now=(1.0-max((db)0.0,(mid*2.0-now))-mid);
	if(now<-eps)return false;
      }
    }
    else {
      if(!p)now=1.0;
      else {
	now=(1.0-max((db)0.0,(mid*2.0-now)));
	if(now<-eps)return false;
      }
    }
    p=1;
  }
  return true;
}
int main(){
  freopen("guess.in","r",stdin);
  freopen("guess.out","w",stdout);
  TC=read();
  while(TC--){
    n=read();a[0]=read();A=read();B=read();C=read();M=read();
    for(LL i=1;i<=n;i++)cnt[i]=0;
    for(LL i=1;i<=n;i++){a[i]=((A*a[i-1]%M*a[i-1]%M+B*a[i-1]%M+C)%M)+1;cnt[a[i]]++;}
    sort(a+1,a+n+1,[&](LL x,LL y){return x<y;});
    db l=0.0,r=1.0;
    for(LL t=1;t<=100;t++){
      db mid=(l+r)*0.5;
      if(check(mid)){l=mid;}
      else {r=mid;}
    }
    printf("%.10Lf ",l);
    LL ans=-1;
    for(LL i=1;i<=(n*3);i++){
      LL j=(LL)(i*l+eps);
      if(fabs((db)1.0*j/i-l)<(1e-9)){ans=j*inv(i)%mod;break;}
    }
    writeln(ans);
  }
  return 0;
}
